<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="分类名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="分类名称"></el-input>
    </el-form-item>
    <el-form-item label="商品类别" prop="type">
      <el-input v-model="dataForm.type" placeholder="商品类别"></el-input>
    </el-form-item>
    <el-form-item label="商品级别" prop="parentId">
      <el-input v-model="dataForm.parentId" placeholder="商品父级"></el-input>
    </el-form-item>
    <el-form-item label="优先级" prop="orderNum">
      <el-input v-model="dataForm.orderNum" placeholder="优先级"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          type: '',
          parentId: '',
          orderNum: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          name: [
            { required: true, message: '分类目录名称不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '分类目录级别，例如1代表一级，2代表二级，3代表三级不能为空', trigger: 'blur' }
          ],
          parentId: [
            { required: true, message: '父id，也就是上一级目录的id，如果是一级目录，那么父id为0不能为空', trigger: 'blur' }
          ],
          orderNum: [
            { required: true, message: '目录展示时的优先级不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http.post("/admin/category/searchCategoryById", {id}).then(resp => {
              if (resp.data.code == 200) {
                this.dataForm.name = resp.data.category.name
                this.dataForm.type = resp.data.category.type
                this.dataForm.parentId = resp.data.category.parentId
                this.dataForm.orderNum = resp.data.category.orderNum
              } else {
                this.$message.error(data.msg)
              }
            });
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            let data = {
              id: this.dataForm.id,
              name: this.dataForm.name,
              type: this.dataForm.type,
              parentId: this.dataForm.parentId,
              orderNum: this.dataForm.orderNum
            };
            this.$http.post(`/admin/category/${!this.dataForm.id ? 'add' : 'update'}`, data).then(resp => {
              if (resp.data.code == 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            });
          }
        })
      }
    }
  }
</script>
